﻿using Factory.Common;
using Factory.RBAC;
using Factory.RBAC.Accounts;
using Factory.RBAC.Accounts.Dto;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Factory.Web.Controllers.RBAC
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    public class AccountController : Controller
    {
        /// <summary>
        /// 账户接口
        /// </summary>
        private readonly IAccountAppService _accountAppService;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="accountAppService"></param>i
        public AccountController(IAccountAppService accountAppService)
        {
            _accountAppService = accountAppService;
        }

        /// <summary>
        /// 首页显示
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        ///  添加视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddView()
        {
            return View();
        }

        /// <summary>
        ///  添加视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateView(int id)
        {
            ViewBag.id = id;
            return View();
        }



        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="accountName">账号</param>
        /// <param name="accountPass">密码</param>
        /// <returns>返回用户的信息</returns>
        [HttpGet]
        public async Task<Account> Log(string? accountName, string? accountPass)
        {
            var accountlist =await _accountAppService.Log(accountName, accountPass);
            return accountlist;
        }


        /// <summary>
        /// 用户显示（分页）
        /// </summary>
        /// <param name="page">页面</param>
        /// <param name="limit">页容量</param>
        /// <param name="accountName">用户名</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> Display(int page, int limit, string? accountName)
        {
            var accountlist = await _accountAppService.Display(page, limit, accountName);
            return Json(new { data = accountlist, code = 0 });

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<int> Delete(int id)
        {
            var accountDelete =await _accountAppService.Delete(id);
            return accountDelete;
        }


        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>返回受影响行数</returns>
        [HttpGet]
        public async Task<Account> Backfill(int id)
        {
            var accountList = await _accountAppService.Backfill(id);
            return accountList;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="account">用户实体</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<int> Add(Account account)
        {
            var accountAdd = await _accountAppService.Add(account);
            return 1;
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="account">修改DTO</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<int> Update(UpdateAccountDto account)
        {
            var accountUpdate = await _accountAppService.Update(account);
            return 1;
        }

        /// <summary>
        /// 获取用户数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> Select()
        {
            var roleList =await _accountAppService.Select();
            return Json(new {data= roleList });
        }


        /// <summary>
        /// 五表联查
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<Menu>> FiveTables(int id)
        {
            var fiveTables = await _accountAppService.FiveTables(id);
            return fiveTables;
        }


    }
}
